<?php
/**
 * 角色修改类
 * User: Administrator
 * Date: 2018\8\31 0031
 * Time: 17:08
 */

namespace app\Lib\Model\Role\RoleManage;


use app\Lib\Exception\BlException;
use app\Lib\Exception\SysException;
use app\Lib\InterfaceIn\Role\RoleHandle;
use app\Lib\InterfaceIn\Role\RoleManage\SaveRoleHandle;
use app\Lib\Model\AdminAuth\GetAuthorization\GetRoleAuth;
use app\Lib\Model\DbModel\AdminRoleAuth;
use app\Lib\Model\Role\Role;

class SaveRole extends RoleOperatingBase implements SaveRoleHandle
{
    /** @var RoleHandle $roleObj */
    protected $roleObj;
    /**
     * 初始化
     * SaveRoleHandle constructor.
     * @param RoleHandle $roleObj
     */
    public function __construct(RoleHandle $roleObj)
    {
        $this->roleObj = $roleObj;
    }

    /**
     * 执行代码
     * @return bool
     * @throws BlException
     * @throws SysException
     */
    public function run()
    {
        $model        = $this->getRoleDb();
        $roleValidate = $this->getRoleValidate();
        $data         =  $this->getHandleArray();
        $data[$model->_save_time] = time();
        $data[$model->_save_uid]  = $this->user->getUid();
        $data[$model->_id]        = $this->roleObj->getRoleId();
        if(!$roleValidate->scene('save')->check($data)){
            throw new BlException($roleValidate->getError());
        }
        if($model->update($data) === false){
            throw new SysException('修改角色失败');
        }
        $this->handelAuth();
        (new GetRoleAuth($this->roleObj))->delCache();
        return true;
    }

    /**
     * 权限处理
     * @throws BlException
     */
    private function handelAuth(){
        $adminRole = (new AdminRoleAuth());
        if(empty($this->authMenu)){
            //删除全部
            $result = $adminRole->where([$adminRole->_role_id=>$this->roleObj->getRoleId()])->delete();
            if(!$result){
                throw new BlException('删除权限失败');
            }
        }else{
            $auth = new GetRoleAuth($this->roleObj);
            $obj  = $auth->run();
            $roleAuth = [];
            foreach ($obj as $v){
                /**@var \app\Lib\Model\AdminAuth\MenuAuth $v */
                $roleAuth[$v->getMenuId()] = $v->getMenuId();
            }
            if(empty($roleAuth)){
                $this->addArray($this->authMenu);
            }else{
//                dump($this->authMenu);
//                dump($roleAuth);
                $addArray = array_diff($this->authMenu,$roleAuth);
                if($addArray){
                    $this->addArray($addArray);
                }
                $delArray = array_diff($roleAuth,$this->authMenu);
                if($delArray){
                    $this->delArray($delArray);
                }
            }
        }
        return true;
    }

    /**
     * @param $auth
     * @return bool
     * @throws BlException
     */
    private function delArray($auth){
        $adminRole = (new AdminRoleAuth());
        foreach ($auth as $v){
            $map = [
                $adminRole->_role_id=>$this->roleObj->getRoleId(),
                $adminRole->_menu_id=>$v
            ];
            $result = $adminRole->where($map)->delete();
            if(!$result){
                throw new BlException('删除部分权限失败');
            }
        }
        return true;
    }

    /**
     * @param $addArray
     * @return bool
     * @throws BlException
     */
    private function addArray($addArray){
        $adminRole = (new AdminRoleAuth());
        foreach ($addArray as $k=>$v){
            $map[$k] = [
                $adminRole->_role_id=>$this->roleObj->getRoleId(),
                $adminRole->_menu_id=>$v,
                $adminRole->_create_time=>time(),
                $adminRole->_create_uid=>$this->user->getUid(),
            ];
        }
        $result = $adminRole->insertAll($map);
        if(!$result){
            throw new BlException('新增部分权限失败');
        }
        return true;
    }
}